# This file creates Figure SI.5

# install.packages("xtable")
# install.packages("ggplot2")
# install.packages("dplyr")

library(xtable)
library(ggplot2)
library(dplyr)

# load estimates for mothers
load("mother_twin_old_20_res.rdata")

# load estimates for fathers
load("father_twin_old_20_res.rdata")

## Create plot for effect of twinning 

# compile estimates in data frame
reduced_tab <- 
  data_frame(parent = rep(c("Fathers",
                            "Mothers"), 
                          each = 3))
reduced_tab <- 
  reduced_tab %>%
  mutate(place  = c(0.9, 1.9, 2.9, 1.1, 2.1, 3.1), 
         year   = rep(c("2009 Municipality",
                      "2013 Municipality",
                      "2014 European"),
                    2),
         effect = c(outtab_dad[[2]][3,1],
                    outtab_dad[[3]][3,1],
                    outtab_dad[[4]][3,1],
                    outtab_mom[[2]][3,1],
                    outtab_mom[[3]][3,1],
                    outtab_mom[[4]][3,1]),
         lower  = effect + qnorm(0.025) * c(outtab_dad[[2]][3,2],
                                            outtab_dad[[3]][3,2],
                                            outtab_dad[[4]][3,2],
                                            outtab_mom[[2]][3,2],
                                            outtab_mom[[3]][3,2],
                                            outtab_mom[[4]][3,2]),
         upper  = effect + qnorm(0.975) * c(outtab_dad[[2]][3,2],
                                            outtab_dad[[3]][3,2],
                                            outtab_dad[[4]][3,2],
                                            outtab_mom[[2]][3,2],
                                            outtab_mom[[3]][3,2],
                                            outtab_mom[[4]][3,2]))

# create plot
plot_twin_reduced <-
  ggplot(reduced_tab, 
         aes(x = effect,
             y = rev(place), 
             xmax = upper,
             xmin = lower, 
             group = parent)) + 
  geom_errorbarh(aes(alpha = parent), height = 0, size = 2) +
  geom_point(aes(alpha = parent), size = 4) + 
  theme_classic() +
  ylab("") + 
  xlab("Effect on Turnout") +
  scale_y_continuous(limits = c(0.5,3.5), breaks = 1:3, labels = rev(c("2009 Municipality\nElection", 
                                                                       "2013 Municipality\nElection",
                                                                       "2014 European\nElection"))) +
  scale_x_continuous(limits = c(-.1,.05)) +
  scale_alpha_discrete(name = "") + 
  geom_vline(xintercept = 0, alpha = 0.5, linetype = 3) +
  theme(axis.text = element_text(size = 14),
        axis.title = element_text(size = 14),
        legend.text = element_text(size = 14),
        legend.title = element_text(size = 14),
        title = element_text(size = 14)) 